package com.company.controller;

import com.company.common.R;
import com.company.entity.Yaopinxinxi;
import com.company.service.YaopinxinxiService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
public class TestController {
    @Resource
    YaopinxinxiService yaopinxinxiService;
    /*
    * 药品信息分页查询
    * */
    @CrossOrigin(origins = "*")
    @RequestMapping("/selectAll/{page}")
    public R show2(@PathVariable("page") int page){
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,5);
        List<Yaopinxinxi> list=this.yaopinxinxiService.list();
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
    * 查询所有
    * */
    @CrossOrigin(origins = "*")
    @RequestMapping("/sasda")
    public R show3(){
        List<Yaopinxinxi> list=this.yaopinxinxiService.list();
        return new R().add("list",list);
    }

    /*
    * 药品信息根据id删除
    * */
    @CrossOrigin(origins = "*")
    @RequestMapping("/del/{id}")
    public R del(@PathVariable("id") int id){
        boolean remove = this.yaopinxinxiService.removeById(id);
        R r=new R();
        if(remove==true){
            r.getMessage();
        }else{
            r.getMessage();
        }
        return r;
    }

    /*
    * 药品信息根据id修改
    * */
    @CrossOrigin(origins = "*")
    @PostMapping("/Update")
    public R Update(@RequestBody Yaopinxinxi yaopinxinxi){
        this.yaopinxinxiService.updateById(yaopinxinxi);
        return new R();
    }

    /*
    * 药品信息根据id查询
    * */
    @CrossOrigin(origins = "*")
    @PostMapping("/selectbyid")
    public R selectbyid(@RequestBody Yaopinxinxi yaopinxinxi){
        Yaopinxinxi yaopinxinxi1 = this.yaopinxinxiService.getById(yaopinxinxi.getId());
        return new R().add("yaopinxinxi1",yaopinxinxi1);
    }

    /*
    *药品信息添加
    * */
    @CrossOrigin(origins = "*")
    @PostMapping("/Insert")
    public R Insert(@RequestBody Yaopinxinxi yaopinxinxi){
        this.yaopinxinxiService.save(yaopinxinxi);
        return new R();
    }

    /*
    * 药品信息图片上传
    * */
    @CrossOrigin(origins = "*")
    @PostMapping("/uploadfile2")
    public R uploadFile2(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\HTML\\Pharmacy\\img\\"+file.getOriginalFilename()));
        return new R().add("fname",file.getOriginalFilename());
    }
}
